package com.wizardOther.Items;

import Validator.Validator;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import popup.PopupController;

/**
 *
 * @author Vi
 */
public class ValidationForm {

    protected List<Boolean> correctField = new ArrayList<>();
    protected ArrayList<Node> inputs = new ArrayList<>();
    protected ArrayList<Label> labels = new ArrayList<>();

    protected void makeList(int size) {
        correctField.clear();

        for (int i = 0; i < size; i++) {
            correctField.add(Boolean.TRUE);
        }
    }

    protected void printValidator(List<Boolean> list) {
        int i = 0;
        for (Boolean b : list) {
            System.out.println(i + " " + b.toString());
            i++;
        }
    }

    protected void checkDouble(String s, int i) {

        if (s.isEmpty()) {
            correctField.set(i, Boolean.FALSE);

        } else {
            try {
                Double.parseDouble(s);
                correctField.set(i, Boolean.TRUE);
            } catch (Exception e) {
                correctField.set(i, Boolean.FALSE);
            }
        }
    }

    protected void checkPesel(String s, int i) {

        if (Validator.checkPesel(s)) {
            correctField.set(i, Boolean.TRUE);
        } else {
            correctField.set(i, Boolean.FALSE);
        }
    }

    protected void checkEmail(String s, int i) {
        if (s.isEmpty()) {
            correctField.set(i, Boolean.FALSE);
        } else {
            if (Validator.isValidEmailAddress(s)) {
                correctField.set(i, Boolean.TRUE);
            } else {
                correctField.set(i, Boolean.FALSE);
            }
        }
    }

    protected void checkInteger(String s, int i) {

        if (s.isEmpty()) {
            correctField.set(i, Boolean.FALSE);

        } else {
            try {
                Integer.parseInt(s);
                correctField.set(i, Boolean.TRUE);
            } catch (Exception e) {
                correctField.set(i, Boolean.FALSE);
            }
        }
    }

    protected void checkDate(String s, int i) {

        if (s.isEmpty()) {
            correctField.set(i, Boolean.FALSE);
        } else {
            try {
                Date.valueOf(s);
                correctField.set(i, Boolean.TRUE);
            } catch (Exception e) {
                correctField.set(i, Boolean.FALSE);
            }
        }
    }

    protected void checkComments(String s, int i) {

        if (s != null) {
            if (s.isEmpty()) {
                correctField.set(i, Boolean.TRUE);
            } else {
                if (s.length() > 100) {
                    correctField.set(i, Boolean.FALSE);
                } else {
                    correctField.set(i, Boolean.TRUE);
                }
            }
        }
    }

    protected void checkNumer(String s, int i, int size) {

        if (s.isEmpty()) {
            correctField.set(i, Boolean.FALSE);
        } else {
            if (s.length() != size) {
                correctField.set(i, Boolean.FALSE);
            } else {
                try {
                    Long.parseLong(s);
                    correctField.set(i, Boolean.TRUE);
                } catch (Exception e) {
                    correctField.set(i, Boolean.FALSE);
                }
            }
        }
    }

    protected void checkString(String s, int i) {

        if (s != null) {
            if (s.isEmpty()) {
                correctField.set(i, Boolean.FALSE);
            } else {
                correctField.set(i, Boolean.TRUE);
            }
        } else {
            correctField.set(i, Boolean.FALSE);
        }

    }

    protected void setWrongTextFields(List<Boolean> textFieldsChecks) {

        int i = 0;
        for (Boolean isGood : textFieldsChecks) {
            if (isGood) {
                if (inputs.get(i).getStyleClass().remove("textFieldWrong")) {
                    inputs.get(i).getStyleClass().add("textField");
                    labels.get(i).setText("");
                }
            } else {

                if (inputs.get(i).getStyleClass().remove("textField")) {
                    inputs.get(i).getStyleClass().add("textFieldWrong");
                    labels.get(i).setText("Złe dane");
                }
            }
            i++;
        }

    }

    protected boolean isCorrect(List<Boolean> list) {
        for (Boolean el : list) {
            if (!el) {
                return false;
            }
        }
        return true;
    }

    protected void createPopup(String text) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("popup.fxml"));
            AnchorPane page = (AnchorPane) loader.load();
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Uwaga!");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);

            PopupController controller = loader.getController();
            controller.setDialogStage(dialogStage);
            controller.setPopupText(text);
            dialogStage.showAndWait();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected String renameCarType(String polish) {
        if (polish == null) {
            return "";
        }
        if (polish.equals("Osobowy")) {
            return "Personal";
        }
        if (polish.equals("Dostawczy")) {
            return "Van";
        }
        if (polish.equals("Motocykl")) {
            return "Motorcycle";
        }
        if (polish.equals("Przyczepka")) {
            return "Trailer";
        }

        return polish;
    }

    protected String renameJobType(String polish) {
        if (polish == null) {
            return "";
        }
        if (polish.equals("Wybierz rodzaj pracy")) {
            return "";
        }
        if (polish.equals("Administrator")) {
            return "admin";
        }
        if (polish.equals("Menadżer")) {
            return "manager";
        }
        if (polish.equals("Księgowy")) {
            return "bookkeeper";
        }
        if (polish.equals("Portier")) {
            return "porter";
        }
        return polish;
    }

    protected String renameJobTypeToPolish(String polish) {
        if (polish == null) {
            return "";
        }
        if (polish.equals("Wybierz rodzaj pracy")) {
            return "";
        }
        if (polish.equals("admin")) {
            return "Administrator";
        }
        if (polish.equals("manager")) {
            return "Menadżer";
        }
        if (polish.equals("bookkeeper")) {
            return "Księgowy";
        }
        if (polish.equals("porter")) {
            return "Portier";
        }
        return polish;
    }

    protected String renamePeyerTypeToPolish(String polish) {
        if (polish == null) {
            return "";
        }
        if (polish.equals("Company")) {
            return "Firma";
        }
        if (polish.equals("PrivatePerson")) {
            return "Osoba fizyczna";
        }
        return polish;
    }
    
      protected String renamePeyerTypeToEnglish(String polish) {
        if (polish == null) {
            return "";
        }
         if (polish.equals("Wybierz typ płatnika")) {
            return "";
        }
        if (polish.equals("Firma")) {
            return "Company";
        }
        if (polish.equals("Osoba fizyczna")) {
            return "PrivatePerson";
        }
        return polish;
    }
      
        protected String renamePassesTypeToPolish(String polish) {
        if (polish == null) {
            return "";
        }
        if (polish.equals("Permanent")) {
            return "Stała";
        }
        if (polish.equals("Temporary")) {
            return "Tymczasowa";
        }
        return polish;
    }
      

    protected String renameCarTypeToPolish(String polish) {
        if (polish == null) {
            return "";
        }
        if (polish.equals("Personal")) {
            return "Osobowy";
        }
        if (polish.equals("Van")) {
            return "Dostawczy";
        }
        if (polish.equals("Motorcycle")) {
            return "Motocykl";
        }
        if (polish.equals("Trailer")) {
            return "Przyczepka";
        }
        if (polish.equals("Wybierz typ pojazdu")) {
            return "";
        }
        return polish;
    }
}
